package com.sgb.kjwl.viewmodel.packers;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.packers.LabourServiceAndEmploymentEntity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author : younghow
 * @date : 2021/7/815:51
 * @desc :
 */
public class LaborServiceAndEmploymentPublishListViewModel extends AndroidViewModel {

    public MutableLiveData<Boolean> loadingLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> successLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> errorLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> mDeleteSuccessLiveData = new MutableLiveData<>();
    public MutableLiveData<List<LabourServiceAndEmploymentEntity>> mServiceLiveData = new MutableLiveData<>();

    public LaborServiceAndEmploymentPublishListViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void getLabourList(Map<String,Object> map){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getLabourList(map).enqueue(new Callback<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>> call, Response<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mServiceLiveData.postValue(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>> call, Throwable t) {

            }
        });
    }

    public void deleteLabour(String no){
        Map<String,Object> map = new HashMap<>();
        map.put("no",no);
        loadingLiveData.postValue(true);
        APIManager.getInstance().getPackerAPI().deleteLabor(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mDeleteSuccessLiveData.postValue(true);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }
}
